<?php
namespace app\manage\controller;
use org\Tree;
class Menu extends Base
{
    //前置操作定义
    protected $beforeActionList = [
        // 'second' =>  ['except'=>'hello'],
        'beforeEdit'  =>  ['only'=>'edit,add'],
    ];
    private $mod;

    public function _initialize(){
        parent::_initialize();
        $this->del_true = true;
        $this->mod = model('menu');
    }
    public function index()
    {
        $tree = new Tree();
        $tree->icon = ['&nbsp;&nbsp;&nbsp;│ ','&nbsp;&nbsp;&nbsp;├─ ','&nbsp;&nbsp;&nbsp;└─ '];
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $result = $this->mod->order('ordid')->select();
        $result = objToArray($result);
        $array = [];
        foreach($result as $r) {
            $r['cname'] = $r['name'];
            $r['str_manage'] = '<a href="javascript:;" class="J_showdialog" data-uri="'.url('Menu/add',['pid'=>$r['id']]).'" data-title="添加子菜单" data-id="add" data-width="500" data-height="350">添加子菜单</a> |
                                <a href="javascript:;" class="J_showdialog" data-uri="'.url('Menu/edit',['id'=>$r['id']]).'" data-title="编辑 - '. $r['name'] .'" data-id="edit" data-width="500" data-height="350">编辑</a> |
                                <a href="javascript:;" class="J_confirmurl" data-acttype="ajax" data-uri="'.url('Menu/delete',['id'=>$r['id']]).'" data-msg="'.sprintf('确定要删除么？',$r['name']).'">删除</a>';
            $array[] = $r;
        }
        $str  = "<tr>
                <td align='center'><input type='checkbox' value='\$id' class='J_checkitem'></td>
                <td align='center'>\$id</td>
                <td>\$spacer<span data-tdtype='edit' data-field='name' data-id='\$id' class='tdedit'>\$name</span>&nbsp;&nbsp;\$module_name/\$action_name</td>
                <td align='center'><span data-tdtype='edit' data-field='ordid' data-id='\$id' class='tdedit'>\$ordid</span></td>
                <td align='center'>\$str_manage</td>
                </tr>";
        $tree->init($array);
        $menu_list = $tree->get_tree(0, $str);
        $this->assign('menu_list', $menu_list);

        $big_menu = [
            'title' => '添加菜单',
            'iframe' => url('Menu/add'),
            'id' => 'add',
            'width' => '500',
            'height' => '350',
        ];
        $this->big_menu = $big_menu;
        $this->assign('big_menu', $big_menu);
        $this->assign('list_table', true);
        return $this->fetch();
    }
    //edit前置操作
    protected function beforeEdit(){
        $id = input('param.id/d');
        $info['pid'] = input('param.pid/d');
        if($id){
            $info = $this->mod->field('id,pid')->find($id);
        }
        $tree = new Tree();
        $result = $this->mod->select();
        $result = objToArray($result);
        $array = array();
        foreach($result as $r) {
            $r['selected'] = $r['id'] == $info['pid'] ? 'selected' : '';
            $array[] = $r;
        }
        $str  = "<option value='\$id' \$selected>\$spacer \$name</option>";
        $tree->init($array);
        $select_menus = $tree->get_tree(0, $str);
        $this->assign('select_menus', $select_menus);
    }
    public function addDo(){
        //注册后置事件 只对模型调用
        \app\manage\model\Menu::event('after_insert',function(){
            $lists = $this->mod->order('ordid')->select();
            cache('managemenu'.session('roleid'),objToArray($lists));
           return true;
        });
        parent::addDo();
    }
    public function editDo(){
        //注册后置事件 只对模型调用
        \app\manage\model\Menu::event('after_update',function(){
            $lists = $this->mod->order('ordid')->select();
            cache('managemenu'.session('roleid'),objToArray($lists));
            return true;
        });
        parent::editDo();
    }
    public function delete(){
        //注册后置事件 只对模型调用
        \app\manage\model\Menu::event('after_delete',function(){
            $lists = $this->mod->order('ordid')->select();
            cache('managemenu'.session('roleid'),objToArray($lists));
            return true;
        });
        parent::delete();
    }
}
